<template>
	<block>
		<hxt-layout id="hxt-layout" title="个人资料" svc="fingerPrint" initTitle="个人资料加载失败" @initPage="initPage">
			<view class="hxt-flex-column" style="background-color: #F5F5F5;" v-if="!!currentUser">
				<van-notice-bar left-icon="volume-o" text="Bucket名称说明" color="#1989fa" background="#ecf9ff" @click="copy('pc/helperShare/726B656D-E6BA-430C-B878-E9771A743B04')"></van-notice-bar>
				<view class="hxt-flex-fill" style="height: 1px;">
					<scroll-view scroll-y style="height: 100%;" :enhanced="true" :bounces="false"
						scroll-with-animation="true" enable-back-to-top="true">
						<view class="text-center padding-top-sm" style="padding-left: 32rpx;padding-right: 32rpx;">
							<view class="bg-white padding-xs">
								<button open-type="chooseAvatar" class="bg-white" v-on:chooseavatar="chooseavatar"
									style="height: 10rem;padding: 0;width: 10rem;">
									<van-image round width="10rem" height="10rem"
										:src="!!currentUser.imgUrl?currentUser.imgUrl:'https://ynz1901.oss-cn-shanghai.aliyuncs.com/main/bear.png'"></van-image>
								</button>
							</view>
						</view>

						<van-cell-group inset title="账号信息">
							<van-cell title="账号" :value="currentUser.account"></van-cell>
							<van-cell title="密码" :value="currentUser.pwd"></van-cell>
						</van-cell-group>
						<van-cell-group inset title="扩展信息">
							<hxt-input label="Bucket名称" v-model="currentUser.bucketName" placeholder="设置后不可修改,请查看说明"
								:border="true" :disabled="user.bucketName"></hxt-input>
							<hxt-input label="姓名" v-model="currentUser.uiName" placeholder="请输入姓名"
								:border="true"></hxt-input>
							<hxt-input label="邮箱" v-model="currentUser.email" :disabled="user.email" placeholder="请输入邮箱"
																:border="true"></hxt-input>
							<hxt-input label="备注" type="textarea" :autosize="{ maxHeight: 100, minHeight: 50 }"
								v-model="currentUser.uiMark" placeholder="请输入备注"></hxt-input>
						</van-cell-group>
					</scroll-view>
				</view>
				<view class="hxt-flex" style="padding: 20rpx;">
					<button class="cu-btn round bg-cyan shadow" @click="submit"
						style="background-color: #1cbbb4;width: 100%;">保存</button>
				</view>
			</view>
		</hxt-layout>
	</block>
</template>
<script>
	import hxt from "@/utils/hxt.js"
	export default {
		data() {
			const app = getApp();
			return {
				list: [],
				old: {
					currentUser: ''
				},
				currentUser: app.globalData.currentUser,
				user: app.globalData.currentUser
			}
		},
		onShow: function(e) {},
		methods: {
			initPage: function(e) { //初始化事件
				var _this = this;
				if (!e.user) e.callBack('个人资料加载失败');
				_this.currentUser = JSON.parse(JSON.stringify(e.user));
				_this.user = e.user;
				_this.old.currentUser = JSON.stringify(_this.currentUser);
			},
			chooseavatar: function(e) {
				var _this = this;
				_this.currentUser.imgUrl = e.detail.avatarUrl;
			},
			copy: function(url) {
				var _this = this;
				wx.setClipboardData({
					data: hxt.domain + url,
					success(res) {
						hxt.Notify({
							type: 'success',
							message: '链接已复制到剪贴板，请粘贴到浏览器打开'
						});
					}
				})
			},
			submit: function(e) {
				var _this = this,
					currentUser = _this.currentUser;
					
				if (_this.old.currentUser == JSON.stringify(currentUser)) return hxt.Notify({
					type: 'warning',
					message: '保存失败：信息未更改'
				});
				if(!currentUser.bucketName) return hxt.Notify({
					type: 'danger',
					message: '请输入Bucket名称'
				});
				if (!(/^[A-Za-z]+$/.test(currentUser.bucketName))) return hxt.Notify({
					type: 'danger',
					message: 'Bucket名称只能输入英文'
				});
				if(!hxt.isEmail(currentUser.email)) return hxt.Notify({
					type: 'danger',
					message: '无效的邮件地址'
				});
				currentUser.optType = 1;
				currentUser.oiId = _this.currentUser.openId;
				var save = function(data) {
					hxt.post({
						url: hxt.domain + "pc/getUserInfo",
						data: data,
						ok: function(res) {
							if (res.data.code != 0) {
								hxt.Notify({
									type: 'danger',
									message: '保存失败：' + res.data.errmsg
								});
								return;
							};
							hxt.Notify({
								type: 'success',
								message: '保存成功',
								duration: 1000
							});
							_this.old.currentUser = JSON.stringify(data);
							_this.user.uiName = _this.currentUser.uiName;
							_this.user.imgUrl = _this.currentUser.imgUrl;
							_this.user.uiMark = _this.currentUser.uiMark;
							_this.user.bucketName= _this.currentUser.bucketName;
							_this.user.email= _this.currentUser.email;
						},
						fail: function(res) {
							hxt.Notify({
								type: 'danger',
								message: '保存失败：' + res.errMsg
							});
						}
					});
				}
				if (!currentUser.imgUrl.startsWith('https://')) {
					hxt.uploadfile({
						path: currentUser.imgUrl,
						ossKey: 'avatar',
						ok: function(res) {
							currentUser.imgUrl = res.data.url;
							save(currentUser);
						},
						fail: function(res) {
							hxt.alert(res.errMsg, '上传文件失败');
						}
					});
					return;
				};
				save(currentUser);

			}
		}
	}
</script>

<style>
	.txt-css {
		word-break: break-all;
		word-wrap: break-word;
		white-space: pre-line;
	}

	.submit-bar {
		position: sticky !important;
		z-index: 0 !important;
	}

	.van-button {
		height: 100% !important;
	}
</style>